package com.zmn.mcc.admin.controller.staff.staffleaveoffice;


import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dictionary.DictModel;
import com.zmn.common.dto.VtDTO;
import com.zmn.mcc.common.constant.StaffConsts;
import com.zmn.mcc.common.constant.StaffLeaveOfficeConsts;
import com.zmn.mcc.common.dictionary.StaffLeaveOfficeDict;
import com.zmn.mcc.model.entity.staff.McStaff;
import com.zmn.mcc.model.entity.staff.McStaffQuery;
import com.zmn.mcc.model.entity.staff.staffleaveoffice.StaffLeaveOffice;
import com.zmn.mcc.model.entity.staff.staffleaveoffice.StaffLeaveOfficeQuery;
import com.zmn.mcc.services.interfaces.staff.staffleaveoffice.StaffLeaveOfficeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 离职信息表基础查询Controller
 *
 * @author tanghongjun
 * @since 2020-05-14 11:15:18
 */
@RestController
@RequestMapping("/staff/staffleaveoffice/")
public class StaffLeaveOfficeListController extends StaffLeaveOfficeBaseController {

    private Logger logger = LoggerFactory.getLogger(StaffLeaveOfficeListController.class);

    @Resource
    private StaffLeaveOfficeService staffLeaveOfficeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("staff/staffleaveoffice/list.action")
    public ModelAndView list(@ModelAttribute StaffLeaveOfficeQuery query) {
        List<StaffLeaveOffice> items = staffLeaveOfficeService.listPageByQuery(query);

        ModelAndView mav = new ModelAndView("staff/staffleaveoffice/list");
        mav.addObject("items", items);
        mav.addObject("vo", query);

        return mav;
    }


    /**
     * 信息
     */
    @RequestMapping("/addEditView")
    //@RequiresPermissions("staff/staffleaveoffice/addEditView.action")
    public ModelAndView addEditView(@RequestParam(value = "staffId", defaultValue = "0") Integer staffId) {

        McStaff staff = staffService.findByKey(staffId);
        StaffLeaveOffice staffLeaveOffice;
        if (staffId == 0) {
            staffLeaveOffice = new StaffLeaveOffice();
        } else {
            staffLeaveOffice = staffLeaveOfficeService.findByStaffId(staffId);
        }
        if (Objects.isNull(staffLeaveOffice)) {
            staffLeaveOffice = new StaffLeaveOffice();
            staffLeaveOffice.setStaffId(staffId);
        }

        McStaffQuery query = new McStaffQuery();
        query.setStatus(GlobalConsts.YES);
        query.setStaffStatus(StaffConsts.STAFF_STATUS_NORMAL);
        query.setPageSize(10000);
        List<McStaff> mcStaffList = Optional.ofNullable(staffService.listSimpleByQuery(query)).orElse(Collections.emptyList());
        List<VtDTO> items = mcStaffList.stream().map(mcStaff -> new VtDTO(mcStaff.getStaffId(), mcStaff.getRealName())).collect(Collectors.toList());

        ModelAndView mav = new ModelAndView("staff/staffleaveoffice/detail");
        mav.addObject("staffLeaveOffice", staffLeaveOffice);
        mav.addObject("obj", staff);
        mav.addObject("staffList", items);
        mav.addObject("leaveReasonList", StaffLeaveOfficeDict.getLeaveReasonList());
        mav.addObject("leaveStatusList", StaffLeaveOfficeDict.getLeaveStatusList());
        mav.addObject("flag", 5);

        return mav;
    }

    /**
     * 描述:操作离职
     *
     * @param staffId 员工的id
     * @return boolean
     * @author tanghongjun
     * @since 9:31 2020/6/3
     **/
    @RequestMapping(value = "leave")
    public ModelAndView leave(@RequestParam(value = "staffId", defaultValue = "0") Integer staffId) {

        McStaff staff = staffService.findByKey(staffId);
        StaffLeaveOffice staffLeaveOffice = StaffLeaveOffice.builder().staffId(staffId).build();

        List<Integer> staffStatusList = Arrays.asList(StaffConsts.STAFF_STATUS_NORMAL, StaffConsts.STAFF_STATUS_PROBATION);
        List<VtDTO> items = Optional.ofNullable(staffService.listVtByStatus(GlobalConsts.YES, staffStatusList, 2000)).orElse(Collections.emptyList());

        ModelAndView mav = new ModelAndView("staff/staffleaveoffice/detail");
        mav.addObject("staffLeaveOffice", staffLeaveOffice);
        mav.addObject("obj", staff);
        mav.addObject("staffList", items);
        mav.addObject("leaveReasonList", StaffLeaveOfficeDict.getLeaveReasonList());
        mav.addObject("leaveStatusList", Arrays.asList(new DictModel(StaffLeaveOfficeConsts.STATUS_LEAVE, StaffLeaveOfficeConsts.STATUS_LEAVE_STR)));
        mav.addObject("flag", 5);

        return mav;

    }

}
